package utils;

import java.io.File;

import javax.swing.JFileChooser;
import javax.swing.filechooser.FileSystemView;

/**
 * This class holds some miscellaneous algorithms that do not yet
 * fit anywhere else.
 */
public class PSAlgorithms {

    /**
     * Find the place to store program settings
     * @return the correct path to the settings file
     */
    public static File getSettingsDirectory() {

        System.out.println("1.1.1.1 Creating JFileChooser");
        JFileChooser fc = new JFileChooser(); //DEADLOCK
        System.out.println("1.1.1.1.1 Getting FileSystemView");
        FileSystemView fsv = fc.getFileSystemView();
        System.out.println("1.1.1.1.2 Getting DefaultDirectory");
        File userDefault = fsv.getDefaultDirectory();
        System.out.println("1.1.1.2 Confirming default directory");
        if(userDefault == null) {
            throw new IllegalStateException("getDefaultDirectory==null");
        }
        System.out.println("1.1.1.3 Creating settings file object");
        File settingsDirectory = new File(userDefault, ".LudoTNG");
        System.out.println("1.1.1.4 Confirming file");
        if(!settingsDirectory.exists()) {
            System.out.println("1.1.1.4-1 File does not exists, creating...");
            if(!settingsDirectory.mkdir()) {
                throw new IllegalStateException(settingsDirectory.toString());
            }
        }
        System.out.println("1.1.1.5 Settings file is: "+settingsDirectory);
        return settingsDirectory;
    }
}
